layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'treetable', 'func'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;
    var func = layui.func;

    /**
     * 行业信息表管理
     */
    var BmsIndustry = {
        tableId: "bmsIndustryTable",
        id: "",
        name: "",
        level: ""
    };

    /**
     * 初始化表格的列
     */
    BmsIndustry.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: 'center',title: 'ID'},
            {field: 'name', align: 'left',sort: true, title: '行业名称'},
            {field: 'pid', align: 'center',sort: true, title: '上级ID'},
            {field: 'sort', align: 'center',sort: true, title: '排序'},
            // {field: 'status', align: 'center',sort: true, title: '启用状态'},
            // {field: 'createBy', align: 'center',sort: true, title: '创建者'},
            {field: 'createTime',align: 'center', sort: true, title: '创建时间'},
            // {field: 'updateBy',align: 'center', sort: true, title: '更新者'},
            // {field: 'updateTime', align: 'center',sort: true, title: '更新时间'},
            // {field: 'remark', align: 'center',sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击菜单树时
     */
    BmsIndustry.onClickMenu = function (e, treeId, treeNode) {
        BmsIndustry.condition.id = treeNode.id;
        BmsIndustry.search();
    };

    /**
     * 点击查询按钮
     */
    BmsIndustry.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        // table.reload(BmsIndustry.tableId, {
            //     where: queryData, page: {curr: 1}
            // });
        BmsIndustry.initTable(BmsIndustry.tableId, queryData);
    };

    /**
     * 弹出添加对话框
     */
    BmsIndustry.openAddDlg = function () {
        func.open({
            title: '添加行业信息表',
            content: Feng.ctxPath + '/bmsIndustry/add',
            tableId: BmsIndustry.tableId,
            height: 800,
            fixed:false
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    BmsIndustry.openEditDlg = function (data) {
        func.open({
            title: '修改行业信息表',
            content: Feng.ctxPath + '/bmsIndustry/edit?id=' + data.id,
            tableId: BmsIndustry.tableId,
            height: 800,
            fixed:false

        });
    };

    /**
     * 导出excel按钮
     */
    BmsIndustry.exportExcel = function () {
        var checkRows = table.checkStatus(BmsIndustry.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BmsIndustry.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/bmsIndustry/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BmsIndustry.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // // 渲染表格
    // var tableResult = table.render({
    //     elem: '#' + BmsIndustry.tableId,
    //     url: Feng.ctxPath + '/bmsIndustry/list',
    //     page: true,
    //     height: "full-158",
    //     cellMinWidth: 100,
    //     cols: BmsIndustry.initColumn()
    // });

    /**
     * 初始化表格
     */
    BmsIndustry.initTable = function (menuId, data) {
        return treetable.render({
            elem: '#' + menuId,
            url: Feng.ctxPath + '/bmsIndustry/listTree',
            where: data,
            page: false,
            height: "full-98",
            cellMinWidth: 100,
            cols: BmsIndustry.initColumn(),
            treeColIndex: 2,
            treeSpid: "0",
            treeIdName: 'id',
            treePidName: 'pid'
        });
    };

    // 渲染表格
    var tableResult = BmsIndustry.initTable(BmsIndustry.tableId);
    $('#expandAll').click(function () {
        treetable.expandAll('#' + BmsIndustry.tableId);
    });
    $('#foldAll').click(function () {
        treetable.foldAll('#' + BmsIndustry.tableId);
    });

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });

    //初始化左侧部门树
    var ztree = new $ZTree("menuTree", "/menu/selectMenuTreeList");
    ztree.bindOnClick(BmsIndustry.onClickMenu);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BmsIndustry.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BmsIndustry.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BmsIndustry.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BmsIndustry.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BmsIndustry.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BmsIndustry.onDeleteItem(data);
        }
    });
});
